// buff_mgr1.c : Defines the entry point for the console application.

#include "stdafx.h"
#include "buff.h"
#include <conio.h>
#include "stdio.h"
#include <process.h>
#include "iostream"
#include <windows.h>

using namespace std;

int  PAGE_SIZE;
BMgr obj;
void menu();
void buff_init();
void users_choice(int);
int _tmain(int argc, _TCHAR* argv[])
{
	/************************* To display the user-menu*******************************/
	
	menu();
	getch();
}

void menu()
{
	system( "cls" );
	/****************************************MENU FOR BUFFER MANAGER*****************************/

	cout<<"\n\n**************************** BUFFER MANAGER ********************************";
	cout<<"\n\n\t\t\t\t MENU\n\n";
	cout<<"__________________________________________________________________________\n";
	cout<<"\n1. Set the Page Size";
	cout<<"\n2. Initialize Buffer";
	cout<<"\n3. Show the Page";
	cout<<"\n4. Create a file";
	cout<<"\n5. Open a file";
	cout<<"\n6. Write a file";
	cout<<"\n7. Read a file";
	cout<<"\n8. Show frame details";
	cout<<"\n9. Display Frame";
	cout<<"\n10. Show Hit Ratio";
	cout<<"\n11. Close Buffer";
	cout<<"\n12. Delete File";
	cout<<"\n13. Close File";
	cout<<"\n__________________________________________________________________________\n";

	/************************* User will enter his choice ****************************/
	int user_choice = -1;

	/************************* Validating choice ************************/

	while(user_choice < 1 || user_choice > 13)
	{
		cout<<"Please enter an option between 1 & 13 ..... ";
		cin>>user_choice;
	}

	/***************** Calling function according to the user's choice ******************/
	users_choice(user_choice);
}

void users_choice(int user_choice)
{
	switch(user_choice)
	{
	case 1:	/*initializing the global variable PAGE_SIZE*/
		system( "cls" );
		cout<<"\n PAGE SIZE \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"\n Please enter the buffer page size (in KB): ";
		cin>>PAGE_SIZE;
		cout<<".";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		cout<<" Page Size initialized to :"<<PAGE_SIZE<<" KB";
		cout<<"\n Press any key to continue";
		getch();
		menu();
		break;

	case 2:	/*initializing the Buffer Pool*/
		buff_init();
		break;

	case 3:
		break;
	case 4:
		break;
	case 5:
		break;
	case 6:
		break;
	case 7:
		char dbn[50];
		system( "cls" );
		cout<<"\n READ PAGE \n";
		cout<<"\n__________________________________________________________________________\n";
		//cout<<"Enter the dbname to be read :";
		//cin>>dbn;
		//obj.readPage(dbn,1);
		obj.readPage("ourdb.dbase",1);
		//readpg.readPage(dbn,1);
		break;
	case 8:
		break;
	case 9:
		break;
	case 10:
		break;
	case 11:
		break;
	case 12:
		break;
	case 13:
		break;
	default:
		break;
	}
}

void buff_init()
{
		long buff_mem;
		system( "cls" );
		cout<<"\n INITIALIZING BUFFER \n";
		cout<<"\n__________________________________________________________________________\n";
		cout<<"Enter the size of the Buffer Memory(in KB) :";
		cin>>buff_mem;

		/*Object to initialize the size of Buffer*/
		//BMgr init_obj;

		/* Calling Method to do allocation */
		//init_obj.init_buffer(buff_mem,PAGE_SIZE);
		obj.init_buffer(buff_mem,PAGE_SIZE);

		cout<<".";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		Sleep(250);
		cout<<" .";
		cout<<"\nAllocation done";
		cout<<"\nBuffer Space :"<<buff_mem<<" KB";
		cout<<"\nPage Size :"<<PAGE_SIZE<<" KB";
		cout<<"\nNumber of frames available :"<<buff_mem/PAGE_SIZE;
		cout<<"\nPress any key to continue";
		getch();
		menu();
}